#ifndef RESOLV_H_
#define RESOLV_H_

#include <sys/socket.h>
#include <ev.h>
#include "udns/udns.h"
#include "dict.h"

#define QUERY_A     0
#define QUERY_AAAA  1

struct addr {
    time_t expir; /* expire time, -1 is non-expire */
    int cnt; /* address count */
    struct {
        union {
            struct in_addr in4;
            struct in6_addr in6;
        };
        sa_family_t family;
    } addr[1]; /* less one, use malloc allocate more */
};

struct resolv;
/* if you want store the domain (char *) or the result (struct addr *),
 * you should store copy of them, do not just store the pointer */
typedef void resolv_cb(const char *domain, const struct addr *result, void *udata);

struct resolv_ctx {
    struct dns_ctx *udns_ctx; /* libudns context */
    struct dict *hosts; /* /etc/hosts */
    struct {
        struct dict *dict;
        time_t expire;
    } cache;
    struct dict *queryings; /* all the requesting queries */
    int query_sock; /* query UDP socket */
    struct ev_loop *evloop;
    ev_io evio;
    ev_timer evtimer;
};


struct resolv_ctx *resolv_ctx_create(time_t dns_cache_expire, struct ev_loop *evloop);
struct resolv *resolv_query(struct resolv_ctx *ctx, const char *domain,
        resolv_cb *cb, void *udata);
void resolv_cancel(struct resolv *resolv, resolv_cb *cb, void *udata);

#endif /* RESOLV_H_ */
